
#ifndef __KNUTIL_H__
#define __KNUTIL_H__

#include <string>
#include <sstream>
#include <algorithm>
#include <cstdio>

namespace knlib {
namespace util {

char ctolower(char c)
{
	if('A'<=c && c<='Z')
		return c+'a'-'A';
	else
		return c;
}

std::string stolower(const std::string& str)
{
	std::string s = str;
	std::transform(s.begin(), s.end(), s.begin(), ctolower);
	return s;
}

std::string itostring(int i)
{
	char buffer[16];
	std::sprintf(buffer, "%d", i);
	return std::string(buffer);
}

int string_to_int(const std::string& str)
{
	return std::atoi(str.c_str());
}

std::string int_to_string(int i)
{
	std::ostringstream s;
	s << i;
	return s.str();
}

template <typename Target, typename Source>
Target str_cast(const Source& arg)
{
	std::stringstream interpreter;
	Target result;
	if( !(interpreter << arg) || !(interpreter >> result) || !(interpreter >> std::ws).eof() )
		;
	return result;
}


}}

#endif


